package cn.wolfcode.web.controller;


import cn.wolfcode.domain.Customer;
import cn.wolfcode.domain.Employee;
import cn.wolfcode.domain.SystemDictionaryItem;
import cn.wolfcode.qo.CustomerQueryObject;
import cn.wolfcode.service.ICustomerService;
import cn.wolfcode.qo.QueryObject;
import cn.wolfcode.qo.JsonResult;
import cn.wolfcode.service.IEmployeeService;
import cn.wolfcode.service.ISystemDictionaryItemService;
import com.github.pagehelper.PageInfo;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authz.annotation.Logical;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

@Controller
@RequestMapping("/customer")
public class CustomerController {

    @Autowired
    private ICustomerService customerService;

    @Autowired
    private IEmployeeService employeeService;

    @Autowired
    private ISystemDictionaryItemService systemDictionaryItemService;


    @RequiresPermissions(value = {"customer:potentialList","客户页面"},logical = Logical.OR)
    @RequestMapping("/potentialList")
    public String list(Model model, @ModelAttribute("qo") CustomerQueryObject qo){

        //  设置状态
        //  只显示潜在客户
        qo.setStatus(Customer.STATUS_COMMON);

        //  获取主体对象
        //  只显示销售人的客户
        Subject subject = SecurityUtils.getSubject();
        //  判断是否为管理员或经理
        if (!(subject.hasRole("ADMIN") || subject.hasRole("Market_Manager"))) {
            //  获取当前登录对象
            Employee employee = (Employee) subject.getPrincipal();
            //  设置销售人id
            qo.setSellerId(employee.getId());
        }

        PageInfo<Customer> pageInfo = customerService.query(qo);
        model.addAttribute("pageResult", pageInfo);

        //  查询下拉框显示的人员是经理或专员
        List<Employee> employees = employeeService.selectByRoleSn("Market", "Market_Manager");
        model.addAttribute("employees", employees);

        //  查询职业
        List<SystemDictionaryItem> jobs = systemDictionaryItemService.selectByParentSn("job");
        model.addAttribute("jobs", jobs);

        //  查询来源
        List<SystemDictionaryItem> sources = systemDictionaryItemService.selectByParentSn("source");
        model.addAttribute("sources", sources);

        //  查询交流方式
        List<SystemDictionaryItem> ccts = systemDictionaryItemService.selectByParentSn("communicationMethod");
        model.addAttribute("ccts", ccts);

        return "customer/potentialList";
    }

/*    @RequestMapping("/delete")
    @RequiresPermissions(value = {"customer:delete","客户删除"},logical = Logical.OR)
    @ResponseBody
    public JsonResult delete(Long id){
        if (id != null) {
            customerService.delete(id);
        }
        return new JsonResult();
    }*/


    @RequestMapping("/saveOrUpdate")
    @RequiresPermissions(value = {"customer:saveOrUpdate","客户新增/编辑"},logical = Logical.OR)
    @ResponseBody
    public JsonResult saveOrUpdate(Customer customer){
        if (customer.getId() != null) {
            customerService.update(customer);
        }else {
            customerService.save(customer);
        }
        return new JsonResult(true, "操作成功");
    }
}
